package org.ant.gui.actionListeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import org.ant.ctrl.CodesManager;
import org.ant.gui.AntFrame;

public class OpenActionListener implements ActionListener {

	private AntFrame frame;

	public OpenActionListener() {
	}

	public void actionPerformed(ActionEvent arg0) {
		System.out.println("OPEN ACTION:");
		openFile();
	}

	private void openFile() {
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File("."));

		chooser.setFileFilter(new FileFilter() {
			public boolean accept(File f) {
				return f.isDirectory()
						|| f.getName().toLowerCase().endsWith(".egg");
			}

			public String getDescription() {
				return "EGG files";
			}
		});

		int r = chooser.showOpenDialog(frame);
		if (r != JFileChooser.APPROVE_OPTION) {
			return;
		}

		String path = chooser.getSelectedFile().getPath();
		
		CodesManager.getInstane().openCode(path);
	}
}
